package cn.Filter;

import cn.entity.Text1;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class PermissionFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain) throws IOException, ServletException {

        HttpServletRequest request = (HttpServletRequest)servletRequest;
        HttpServletResponse response = (HttpServletResponse)servletResponse;

        HttpSession session = request.getSession();
            //如果为null则说明session还未创建，跳转到登录页面
//        Object object = session.getAttribute("text1");
        Object text11 = session.getAttribute("text1");
        //如果为null则说明还没有登录成功，跳转到登录页面
        Text1 text1 = null;
        if(text11 != null) {
            text1 = (Text1) text11;
        }
        if(text1.getUsername()!=null){
            chain.doFilter(servletRequest,servletResponse);
        } else {
        response.sendRedirect("http://localhost:8080/denglu.jsp");
        //request.getRequestDispatcher("index2.jsp").forward(request,response);



    }

           //request.getRequestDispatcher("denglu.jsp").forward(request,response);
           // response.sendRedirect("http://localhost:8080/system/InsertManege.jsp");

    }

    @Override
    public void destroy() {

    }
}
